<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Nats\Consumer;
use Hyperf\Nats\AbstractConsumer;
use Hyperf\Nats\Annotation\Consumer;
use Hyperf\Nats\Message;
use App\Utils\ServerUtil;
use App\Logic\Coupon;
/**
 *
 */
class CouponConsumer extends AbstractConsumer
{
    public function consume(Message $payload)
    {
        $data = $payload->getBody();         
        $data = json_decode($data,true); 
        ServerUtil::info('Nats-CouponConsumer-进入', ['param'=>$data]);
        $type = $data['type'] ?? '';
        if(!\is_array($data)||empty($type)){
            $payload->reply(json_encode(ServerUtil::nats(1001,'请求参数不对')));  
            return;
        }        
        $obj = $this->container->get(Coupon::class);
        //开协程处理,防止进程阻塞下个逻辑
        co(function () use ($obj,$payload,$data,$type){
            switch ($type)
            {
                case 1: 
                    //根据条件获取可红包信息
                    $res = $obj->getCouponByCondition($data['openid'],$data['coupon_id'],2);
                    ServerUtil::info('Nats-CouponConsumer-根据条件获取可红包信息',['param'=>$data,'res'=>$res]);
                    $payload->reply(json_encode(ServerUtil::nats($res['code'],$res['msg'],$res['data']))); 
                    break;
                case 2: 
                    //标记红包使用
                    $res = $obj->updateCoupon($data['openid'],$data['coupon_id'],1,$data['order_no']??"");
                    ServerUtil::info('Nats-CouponConsumer-标记红包使用',['param'=>$data,'res'=>$res]);
                    $payload->reply(json_encode(ServerUtil::nats($res['code'],$res['msg'])));  
                    break;
                case 3: 
                    //领取红包
                    $res = $obj->updateCoupon($data['openid'],$data['coupon_id'],2,$data['order_no']??"");
                    ServerUtil::info('Nats-CouponConsumer-领取红包',['param'=>$data,'res'=>$res]);
                    $payload->reply(json_encode(ServerUtil::nats($res['code'],$res['msg'])));  
                break;
                case 4: 
                    //退款红包
                    $res = $obj->updateCoupon($data['openid'],$data['coupon_id'],3,$data['order_no']??"");
                    ServerUtil::info('Nats-CouponConsumer-退款红包',['param'=>$data,'res'=>$res]);
                    $payload->reply(json_encode(ServerUtil::nats($res['code'],$res['msg'])));  
                    break;
                case 5: 
                    //根据条件获取红包信息
                    $res = $obj->getCouponByCondition($data['openid'],$data['coupon_id']);
                    ServerUtil::info('Nats-CouponConsumer-根据条件获取红包信息',['param'=>$data,'res'=>$res]);
                    $payload->reply(json_encode(ServerUtil::nats($res['code'],$res['msg'],$res['data'])));  
                    break;
                default:
                    $payload->reply(json_encode(ServerUtil::nats(600,'type解析失败'))); 
            }
        });
        return;
    }
}